<template>
  <section class="chat">
    <div class="chat-eara">
      <div class="chat-content">
        <div v-for="(item,index) in msgList" :key="index" class="msg-item">
          <div v-if="item.userType==='0'" class="msg-my">
            <div class="msg-text">{{item.text}}</div>
            <div class="nickname">{{item.nickname}}</div>
          </div>
          <div v-else class="msg-other">
            <div class="nickname">{{item.nickname}}</div>
            <div class="msg-text">{{item.text}}</div>
          </div>
        </div>
      </div>
      <div class="chat-send">
        <input type="text" v-model="message" class="send-input" placeholder="请输入内容" @keydown.enter="sendMsg"><button class="send-btn" @click="sendMsg">发送</button>
      </div>
    </div>
  </section>
</template>

<script>
import imClient from './components/utils/im-client.js'
export default {
  data () {
    return {
      message: '',
      msgList: []
    }
  },
  mounted () {
    this.messageEvent()
  },
  methods: {
    /**
     * @author ZhenYuTsai
     * @description 发生聊天信息事件
     * @return {*}
     * @lastEditors ZhenYuTsai
     */
    async sendMsg () {
      if (this.message) {
        const res = await imClient.sendDefaultMessage({ text: this.message })
        this.msgList.push({
          userType: '0',
          text: res.data.message.payload.text,
          nickname: res.data.message.nick || '我'
        })
        this.message = ''
      }
    },
    /**
     * @author ZhenYuTsai
     * @description 接收聊天信息回调
     * @lastEditors ZhenYuTsai
     */
    messageEvent () {
      imClient.$on('TEXT_MESSAGE_GROUP', (res) => {
        if (res.length > 0) {
          res.forEach(item => {
            this.msgList.push({
              userType: '1',
              text: item.payload.text,
              nickname: item.nick || '客户'
            })
          })
        }
      })
    }
  },
  beforeDestroy () {
    // 销毁TEXT_MESSAGE_GROUP监听
    imClient.$off('TEXT_MESSAGE_GROUP')
  }
}
</script>

<style>
.chat{
  width: 800px;
  height: 320px;
  padding: 20px;
  box-sizing: border-box;
}
.chat .chat-eara{
  border: 1px solid #ebeef5;
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  height: 100%;
  background-color: #f5f5f5;
}
.chat .chat-eara .chat-content{
  padding: 30px;
  box-sizing: border-box;
  height: calc(100% - 40px);
  overflow-y: auto;
}
/*滚动条样式*/
.chat .chat-content::-webkit-scrollbar {
    width: 4px;
    /*height: 4px;*/
}
.chat .chat-content::-webkit-scrollbar-thumb {
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
    background: rgba(0,0,0,0.2);
}
.chat .chat-content::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
    border-radius: 0;
    background: rgba(0,0,0,0.1);
}
.chat .chat-eara .chat-content .msg-item + .msg-item{
  margin-top: 15px;
}
.chat-eara .chat-content .msg-my{
  display: flex;
  justify-content: flex-end;
}
.chat-eara .chat-content .msg-my .msg-text{
  position: relative;
  max-width: 400px;
  min-height: 30px;
  overflow-wrap: break-word;
  padding: 10px;
  box-sizing: border-box;
  background-color: #9eea6a;
  border-radius: 3px;
  text-align: left;
  margin-right: 15px;
}
.chat-eara .chat-content .msg-my .msg-text::before{
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  background-color: #9eea6a;
  top: 10px;
  right: -5px;
  transform:rotate(45deg);
}
.chat-eara .chat-content .msg-other{
  display: flex;
  justify-content: flex-start;
}
.chat-eara .chat-content .msg-other .msg-text{
  position: relative;
  max-width: 400px;
  min-height: 30px;
  overflow-wrap: break-word;
  padding: 10px;
  box-sizing: border-box;
  background-color: #fff;
  border: 1px solid #ededed;
  border-radius: 3px;
  text-align: left;
  margin-left: 15px;
}
.chat-eara .chat-content .msg-other .msg-text::before{
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  background-color: #fff;
  top: 10px;
  left: -5px;
  transform:rotate(45deg);
  border-left: 1px solid #ededed;
  border-bottom: 1px solid #ededed;
}

.chat .chat-eara .chat-send{
  padding-top: 5px;
  height: 40px;
  text-align: center;
}
.chat-eara .chat-send .send-input{
  outline: none;
  border: none;
  background-color: transparent;
  height: 30px;
  width: 300px;
  line-height: 30px;
  padding: 0 15px;
  border-radius: 3px;
  border: 1px solid #dcdfe6;
}
.chat-eara .chat-send .send-btn{
  outline: none;
  border: none;
  background-color: transparent;
  height: 30px;
  line-height: 30px;
  background-color: #1890ff;
  padding: 0 15px;
  border-radius: 3px;
  color: #fff;
  margin-left: 20px;
}
</style>
